﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using MapDownloader.Core.Core;
using MapDownloader.Core.Models;
using MapDownloader.Core.Storage;
using Microsoft.AspNetCore.Mvc;

namespace MapDownLoaderWeb.Controllers
{
   public class MapController : Controller
   {
      [HttpGet]
      [Route("/map/{X}/{y}/{z}")]
      public IActionResult Get(int x,int y,int z)
      {
         //Maps.Instance.Provider.Storager = new MongoTileStorage();
         //Maps.Instance.Provider.Storager.SetConnectionString("mongodb://127.0.0.1:27017/?safe=true");
         
          BaseImage img = Maps.Instance.GetImageFrom(x,y,z);
         if (img != null)
         {
            return File(img.Data, "image/jpeg");
         }
         return new EmptyResult();
      }
      [HttpGet]
      [Route("/download/{minLat}/{minLng}/{maxLat}/{maxLng}")]
      public JsonResult Download(double minLat, double minLng, double maxLat, double maxLng)
      {
         Task.Factory.StartNew(() => Maps.Instance.AutoStoreTile(minLat, minLng, maxLat, maxLng));
         return Json(new { ok = true });
      }
      [HttpGet]
      [Route("/test/{minLat}/{minLng}/{maxLat}/{maxLng}")]
      public JsonResult Test(double minLat, double minLng, double maxLat, double maxLng)
      {
         return Json(new { minLat, minLng, maxLat, maxLng });
      }
   }
}
